import 'dart:core';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_app/class/MyApp8.dart';


class MyApp9 extends StatelessWidget {
  const MyApp9({super.key});

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      title: '样式MyApp9',
      theme: ThemeData(
          primarySwatch: Colors.deepPurple,
          appBarTheme: const AppBarTheme(
            color: Colors.deepPurple
          )
      ),
      home: const Signature(),
    );
  }
}

class Signature extends StatefulWidget {
  const Signature({super.key});

  @override
  State<Signature> createState() => SignatureState();
}

class SignatureState extends State<Signature> {
  final List<Offset> _points = <Offset>[];
  late Offset _startPoint = Offset.zero;
  late Offset _endPoint = Offset.zero;

  /*获取结束点后添加画线*/
  void _addLin({required double endX,required double endY}){
      setState(() {
        //已存在，则移除
        _points.removeWhere((item){
          return (item.dx == endX && item.dy == endY);
        });

        _points.add(Offset(endX,endY));
      });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        title: const Text('样式MyApp9-画板'),
        foregroundColor: Colors.white,
        actions: [
          IconButton(onPressed: (){
            Navigator.push(context, MaterialPageRoute(builder:(context) => const MyApp8()));
          },
              icon: const Icon(Icons.arrow_right)),
        ],
      ),
      body: GestureDetector(
        //按下
        onPanDown: (DragDownDetails details){
          RenderBox renderBox = context.findRenderObject() as RenderBox;
          Offset localPosition = renderBox.localToGlobal(details.localPosition);
          debugPrint('按下时的坐标:${localPosition.dx},${localPosition.dy}');

          setState(() {
            _startPoint = localPosition;
          });
        },

        //移动
        onPanUpdate: (DragUpdateDetails details){
          setState(() {
              RenderBox renderBox = context.findRenderObject() as RenderBox;
              Offset localPosition = renderBox.localToGlobal(details.localPosition);
              debugPrint('移动时的坐标:${localPosition.dx},${localPosition.dy}');
              _addLin(endX: localPosition.dx, endY: localPosition.dy);
          });
        },

        //松开
        onPanEnd: (DragEndDetails details) {
          RenderBox renderBox = context.findRenderObject() as RenderBox;
          Offset localPosition = renderBox.localToGlobal(details.localPosition);
          debugPrint('松开时的坐标:${localPosition.dx},${localPosition.dy}');

          setState(() {
            _endPoint = localPosition;
          });
        },

        child: CustomPaint(painter: SignaturePainter(_startPoint,_endPoint,_points),size: Size.infinite),
      ),
    );
  }

}


class SignaturePainter extends CustomPainter {
  SignaturePainter(this.startPoint,this.endPoint,this.points);

  final Offset startPoint;
  final Offset endPoint;
  final List<Offset> points;

  @override
  void paint(Canvas canvas,Size size){
    var paint = Paint()
      ..color = Colors.white
      ..strokeCap = StrokeCap.round
      ..strokeWidth = 5.0;

    canvas.drawPoints(PointMode.points, points, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }
}